package com.nccsjz.back.link.web;

import java.sql.SQLException;
import java.util.List;

import com.nccsjz.back.link.service.LinkService;
import com.nccsjz.base.BaseAction;
import com.nccsjz.pojo.Link;

@SuppressWarnings("serial")
public class LinkAction extends BaseAction {
	
	/** links List<Link> 友情链接列表 */
	private List<Link> links;	
	
	/** 封装表单提交的友情链接信息 */
	private Link link;
	
	
	/**
	 * 处理显示友情链接列表的操作
	 * 
	 * @return 友情链接列表页跳转逻辑路径
	 * @throws SQLException 
	 */
	public String listLinks() throws SQLException{
		links = new LinkService().finalAllLinks();
		return "linklist";
	}
	
	/**
	 * goToAddLink方法处理跳转至添加友情链接页面
	 * 
	 * @return
	 */
	public String goToAddLink(){
		return "linkadd";
	}
	
	/**
	 * goToEditLink方法处理跳转至编辑友情链接页面
	 * 
	 * @return 
	 * @throws SQLException 
	 */
	public String goToEditLink() throws SQLException{
		link = new LinkService().findLinkById(link.getId());
		return "linkedit";		
	}
	
	/**
	 * 
	 * @return
	 * @throws SQLException 
	 */
	public String saveLink() throws SQLException{		
		LinkService linkService = new LinkService();
		//添加友情链接
		linkService.addLink(link);
		addActionMessage("友情链接添加成功!");		
		//查询添加后的友情链接
		links = linkService.finalAllLinks(); 
		return "linklist";
	}

	/**
	 * editLink方法处理更新友情链接的操作
	 * 
	 * @return
	 * @throws SQLException
	 */
	public String editLink() throws SQLException{
		LinkService linkService = new LinkService();
		//编辑友情链接
		linkService.editLink(link);
		//查询更新后的友情链接
		links = linkService.finalAllLinks();
		addActionMessage("友情链接更新成功!");
		return "linklist";
	}
	
	/**
	 * deleteLink方法用于处理删除友情链接的操作
	 * 
	 * @return
	 * @throws SQLException 
	 */
	public String deleteLink() throws SQLException{
		LinkService linkService = new LinkService();
		//删除友情链接
		linkService.removeLinkById(link.getId());
		//查询删除后的友情链接列表
		links = linkService.finalAllLinks();
		addActionMessage("友情链接删除成功!");
		return "linklist";
	}

	public List<Link> getLinks() {
		return links;
	}


	public void setLinks(List<Link> links) {
		this.links = links;
	}

	public Link getLink() {
		return link;
	}

	public void setLink(Link link) {
		this.link = link;
	}

}
